package com.ltu.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;



public class ListToTreeUtil<T extends ParentTreeEntity> {
	 
	public static List<ParentTreeEntity> TreeAll = null;
	
	private  Map<Integer, List<ParentTreeEntity>> maps = null;
	private List<ParentTreeEntity> all = null;

	public ListToTreeUtil() {
		super();
	}
	public ListToTreeUtil(List<ParentTreeEntity> all) {
		this.all = all;
	}
//
//	/**
//	 * 将一维数组转换成树型结构
//	 *  to @see getRootTree(List<ParentTreeEntity> source, Integer firstNode) 
//	 */
//	 @Deprecated
//	public List<ParentTreeEntity> getRootTree(String curNode) {
//		if (CollectionUtils.isEmpty(all))
//			return all;
//		List<ParentTreeEntity> rootS = all.stream().filter(item -> item.getParentId().toString().equals(curNode))
//				.collect(Collectors.toList());
//		Set<Integer> filterIds = rootS.stream().map(a -> Integer.parseInt(a.getId())).collect(Collectors.toSet());
//		rootS.forEach(tree -> buildTree(tree, filterIds));
//		return rootS;
//	}
//
//	@Deprecated
//	private void buildTree(ParentTreeEntity tree, Set<Integer> filterIds) {
//		List<ParentTreeEntity> childers = Lists.newArrayList();
//		all.stream().filter(item -> !filterIds.contains(Integer.parseInt(item.getId())))
//				.filter(item -> item.getParentId() == Integer.parseInt(tree.getId()))
//				.forEach(item -> {
//					filterIds.add(Integer.parseInt(item.getId()));
//					buildTree(item, filterIds);
//					childers.add(item);
//				});
//		tree.setChildren(childers);
//	}

	/**
	 * 
	 * @param source 某个节点下的所有子孙节点记录
	 * @param firstNode 起始节点ID
	 * @return 返回指定节点的树结构数据
	 */
	public List<ParentTreeEntity> getRootTree(List<ParentTreeEntity> source, Integer firstNode) {
		this.maps = source.stream().collect(Collectors.groupingBy(ParentTreeEntity::getParentId));
		List<ParentTreeEntity> roots = maps.get(firstNode);
		if(CollectionUtils.isEmpty(roots)) 
			return null;
		setchild(roots);
		return roots;
	}
	
	private void setchild(List<ParentTreeEntity> currentNode) {
		for (ParentTreeEntity item : currentNode) {
			List<ParentTreeEntity> nextNodes = maps.get(Integer.parseInt(item.getId()));
			if (CollectionUtils.isNotEmpty(nextNodes)) {
				setchild(nextNodes);
			}
			item.setChildren(nextNodes);
		}
	}
	
	/**
	 * 
	 * @param source 某个节点下的所有子孙节点记录
	 * @param firstNode 起始节点ID
	 * @return 返回 某个节点的所有子孙节点的一维结构,分割
	 */
	public String getRootTreeStr(List<ParentTreeEntity> source, Integer firstNode) {
		this.maps = source.stream().collect(Collectors.groupingBy(ParentTreeEntity::getParentId));
		List<ParentTreeEntity> roots = maps.get(firstNode);
		if(CollectionUtils.isEmpty(roots)) 
			return Sperators+firstNode.toString();
		StringBuilder   sb = new StringBuilder(firstNode.toString());
		setchildStr(roots,sb);
		return sb.toString();
	}
	private static final String Sperators=",";
	
	private void setchildStr(List<ParentTreeEntity> currentNode,StringBuilder sb) {
		for (ParentTreeEntity item : currentNode) {
			List<ParentTreeEntity> nextNodes = maps.get(Integer.parseInt(item.getId()));
			if (CollectionUtils.isNotEmpty(nextNodes)) {
				setchildStr(nextNodes,sb);
			}
			sb.append(Sperators).append(item.getId());
		}
	}
	

}
